package org.i1510.gen.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.commons.io.IOUtils;
import org.i1510.common.utils.web.ResultMapper;
import org.i1510.gen.bean.ConfigBean;
import org.i1510.gen.service.IGenService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(value = "/admin/gen")
public class GenController {
	private static final Logger logger = LoggerFactory
			.getLogger(GenController.class);

	@Autowired
	private IGenService iGenService;

	/**
	*@Author Rambo
	*@Desc：代码生成器：先使用sql脚本创建然后自动创建代码
	*@link：http://127.0.0.1:8099/i1510_v2/admin/gen/genCode/sys_user.do?packageName=org.i1510.team&author=bobolnear@163.com&version=V1.0
	*@param response
	*@param tableName
	*@throws IOException TODO
	*@Version  GenController.java,v 1.1 2018年8月31日 上午10:22:55 
	*/
	@RequestMapping(value = "/genCode/{tableName}", method = {
			RequestMethod.GET, RequestMethod.POST })
	public void genCode(HttpServletResponse response,
			@Valid @ModelAttribute("configBean") ConfigBean configBean,
			@PathVariable("tableName") String tableName)
			throws IOException {
		ResultMapper mapperRtn = ResultMapper.generateFailure();
		byte[] data = iGenService.generatorCode(tableName,configBean);
		response.reset();
		response.setHeader("Content-Disposition",
				"attachment; filename=\"i1510_code.zip\"");
		response.addHeader("Content-Length", "" + data.length);
		response.setContentType("application/octet-stream; charset=UTF-8");

		IOUtils.write(data, response.getOutputStream());
	}
}